﻿using System;
using System.Windows.Forms;

namespace SimpleCalculator
{
    public partial class CalculatorForm : Form
    {
        private string currentInput = "";
        private double firstOperand = 0;
        private char currentOperator = '\0';
        private bool newInputNeeded = true;

        public CalculatorForm()
        {
            InitializeComponent();
        }

        private void NumberButton_Click(object sender, EventArgs e)
        {
            if (newInputNeeded)
            {
                txtDisplay.Text = "";
                newInputNeeded = false;
            }

            Button button = (Button)sender;
            currentInput += button.Text;
            txtDisplay.Text += button.Text;
        }

        private void OperatorButton_Click(object sender, EventArgs e)
        {
            Button button = (Button)sender;

            if (!newInputNeeded)
            {
                if (currentOperator != '\0')
                {
                    CalculateResult();
                }

                firstOperand = double.Parse(txtDisplay.Text);
                currentInput = "";
            }

            currentOperator = button.Text[0];
            txtDisplay.Text = $"{firstOperand} {currentOperator} ";
            newInputNeeded = true;
        }

        private void EqualsButton_Click(object sender, EventArgs e)
        {
            if (currentOperator != '\0' && !newInputNeeded)
            {
                CalculateResult();
                currentOperator = '\0';
            }
        }

        private void ClearButton_Click(object sender, EventArgs e)
        {
            txtDisplay.Text = "0";
            currentInput = "";
            firstOperand = 0;
            currentOperator = '\0';
            newInputNeeded = true;
        }

        private void CalculateResult()
        {
            double secondOperand = double.Parse(currentInput);
            double result = 0;
            bool error = false;

            switch (currentOperator)
            {
                case '+':
                    result = firstOperand + secondOperand;
                    break;
                case '-':
                    result = firstOperand - secondOperand;
                    break;
                case '×':
                    result = firstOperand * secondOperand;
                    break;
                case '÷':
                    if (secondOperand != 0)
                    {
                        result = firstOperand / secondOperand;
                    }
                    else
                    {
                        error = true;
                    }
                    break;
            }

            if (error)
            {
                txtDisplay.Text = "错误：除数为零";
            }
            else
            {
                // 如果结果是整数，则显示整数形式，否则显示小数
                txtDisplay.Text = result % 1 == 0 ?
                    $"{firstOperand} {currentOperator} {secondOperand} = {result}" :
                    $"{firstOperand} {currentOperator} {secondOperand} = {result:F2}";
            }

            firstOperand = result;
            currentInput = "";
            newInputNeeded = true;
        }
    }
}